<?php
/**
* @package Frontend-User-Access (com_frontenduseraccess)
* @version 3.0.8
* @copyright Copyright (C) 2008 Carsten Engel. All rights reserved.
* @license GPL versions free/trial/pro
* @author http://www.pages-and-items.com
* @joomla Joomla is Free Software
*/

// no direct access
defined('_JEXEC') or die('Restricted access');

jimport( 'joomla.application.component.view');

class frontenduseraccessViewComponents extends JView
{
	function display($tpl = null)
	{
		$database = JFactory::getDBO();
		$controller = new frontenduseraccessController();	
		$this->assignRef('controller', $controller);	
		
		//toolbar			
		JToolBarHelper::custom( 'components_save', 'save.png', 'save_f2.png', JText::_('save'), false, false );	
		
		//get usergroups from db
		$fua_usergroups = $controller->get_usergroups();
		$this->assignRef('fua_usergroups', $fua_usergroups);
		
		//get components access from db
		$controller->db->setQuery("SELECT component_groupid FROM #__fua_components");
		$access_components = $controller->db->loadResultArray();
		$this->assignRef('access_components', $access_components);
		
		//get limits			
		global $mainframe, $option;
		$limit = $mainframe->getUserStateFromRequest( 'global.list.limit', 'limit', $mainframe->getCfg('list_limit'), 'int' );
		$limitstart = $mainframe->getUserStateFromRequest( $option.'.limitstart', 'limitstart', 0, 'int' );		
		$limit_query = "";
		if($limit!=0){
			$limit_query = "LIMIT ".$limitstart.", ".$limit;
		}	
		
		//do filter stuff
		$where = array();		
		$search = $controller->get_var('search');
		if($search){
			$where[] = 'name LIKE '.$controller->db->Quote( '%'.$controller->db->getEscaped( $search, true ).'%', false ).' ';
		}			
		$where = ( count( $where ) ? ' WHERE ' . implode( ' AND ', $where ) : '' );	
		
		//get components from database												
		$controller->db->setQuery( "SELECT SQL_CALC_FOUND_ROWS * "
		. "\nFROM #__components "				
		. $where
		. "\nORDER BY name "
		. $limit_query
		);				
		$components_db_all = $controller->db->loadObjectList();	
		$this->assignRef('components_db_all', $components_db_all);	
		
		//get total from db just for pagination
		$controller->db->setQuery('SELECT FOUND_ROWS();');
		$total = $controller->db->loadResult();
		
		//get pagination stuff
		jimport( 'joomla.html.pagination' );
		$pagination = new JPagination($total,$limitstart,$limit);
		$this->assignRef('pagination', $pagination);
		
		$this->assignRef('search', $search);		

		parent::display($tpl);
	}
}
?>